home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / ADLSORT.ICN < prev    next >
Text File  |  1992-09-28  |  2KB  |  89 lines

  1. ############################################################################
  2. #
  3. #    File:     adlsort.icn
  4. #
  5. #    Subject:  Program to sort address list entries
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 30, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #    This program sorts entries in address lists.  The options are:
  14. #
  15. #    -c    by country
  16. #    -n    by name
  17. #    -z    by ZIP code
  18. #
  19. #     The default is -n.  If more than one option is specified, the
  20. #    order of dominance is -n -z -c.
  21. #
  22. ############################################################################
  23. #
  24. #  See also: address.doc, adlcount.icn, adlfilter.icn, adllist.icn,
  25. #     adlsort,icn, labels.icn
  26. #
  27. #  Links: adlutils, options, namepfx
  28. #
  29. ############################################################################
  30.  
  31. link adlutils, options, namepfx
  32.  
  33. procedure main(args)
  34.    local item, item_lists, opts, sort_method, get_item, add, names, prefixes
  35.    local prefix
  36.  
  37.    item_lists := table()
  38.  
  39.    sort_method := "n"            # The default is sorting by name.
  40.    get_item := get_lastname
  41.  
  42.    opts := options(args,"cnz")
  43.  
  44.    if \opts["c"] then {            # If more than one given, last applies.
  45.       sort_method := "c"
  46.       get_item := get_country
  47.       }
  48.    if \opts["z"] then {
  49.       sort_method := "z"
  50.       get_item := get_zipcode
  51.       }
  52.    if \opts["n"] then {
  53.       sort_method := "n"
  54.       get_item := get_lastname
  55.       }
  56.  
  57.    while add := nextadd() do {
  58.       item := get_item(add)
  59.       /item_lists[item] := []
  60.       put(item_lists[item],add)
  61.       }
  62.        
  63.    item_lists := sort(item_lists,3)
  64.  
  65.    if sort_method == ("c" | "z") then {
  66.       while get(item_lists) do
  67.          every writeadd(!get(item_lists))
  68.       }
  69.  
  70.    else if sort_method == "n" then {
  71.       while get(item_lists) do {
  72.          names := get(item_lists)
  73.          if *names = 1 then writeadd(names[1])    # avoid flap for common case
  74.          else {
  75.             prefixes := table()
  76.             every add := !names do {
  77.                prefix := namepfx(add.text)
  78.                /prefixes[prefix] := []
  79.                put(prefixes[prefix],add)
  80.                }
  81.             prefixes := sort(prefixes,3)
  82.             while get(prefixes) do
  83.                every writeadd(!get(prefixes))
  84.             }
  85.          }
  86.       }
  87.  
  88. end
  89.